<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handlecreateForm({})" v-auth="'btn:add:jieshuizuoye'">
          <Icon icon="fluent:add-12-filled" /> {{ t('发起申请') }}
        </a-button>

        <!-- <a-button type="primary" @click="handlemoreForm()" v-auth="'process:job:pmConnectwaterjob:edit'">
          <Icon icon="clarity:note-edit-line" /> {{ t('完善信息') }}
        </a-button>

        <a-button type="primary" @click="handlesendOaForm()" v-auth="'process:job:pmConnectwaterjob:edit'">
          <Icon icon="clarity:note-edit-line" /> {{ t('推送OA审批') }}
        </a-button>

        <a-button type="primary" @click="handleForm()" v-auth="'process:job:pmConnectwaterjob:edit'">
          <Icon icon="clarity:note-edit-line" /> {{ t('OA审批') }}
        </a-button>

        <a-button type="primary" @click="handleresultForm()" v-auth="'process:job:pmConnectwaterjob:edit'">
          <Icon icon="clarity:note-edit-line" /> {{ t('上传接水结果') }}
        </a-button> -->
      </template>

      


      <template #createDate="{ record }">
          {{ formatDate(record.createDate) }}
      </template>
      <template #connectwatertime="{ record }">
        {{formatDate(record.connectwatertime)}}
      </template>
      <template #sponsordate="{ record }">
        {{formatDate(record.sponsordate)}}
      </template>

      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.projectname }}
        </a>
      </template>

      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)">
          <span style="text-decoration: underline; margin-right: 5px">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)">
          {{ record.projectno }}
        </a>
        
      </template>
    </BasicTable>
    <createForm @register="registercreateDrawer" @success="handleSuccess" />
    <moreForm @register="registermoreDrawer" @success="handleSuccess" />
    <sendOaForm @register="registersendOaDrawer" @success="handleSuccess" />
    <resultForm @register="registerresultDrawer" @success="handleSuccess" />
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <OAForm @register="registerOADrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />

    <component
      :is="inputFormComponent"
      v-model:visible="inputFormVisible"
      @register="registerDrawerBpmMyTask"
      @success="handleSuccess"
    />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProcessJobPmConnectwaterjobList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref,ComponentOptions,shallowRef, onMounted, watch } from 'vue';
  import { bpmTaskDefForm } from '/@/api/bpm/myTask';
  import { dynamicImport } from '/@/router/helper/routeHelper';
  import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  import qs from 'qs';

  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    pmConnectwaterjobDelete,
    pmConnectwaterjobListData,
  } from '/@/api/process/job/pmConnectwaterjob';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import createForm from './createForm.vue';
  import moreForm from './moreForm.vue';
  import sendOaForm from './sendOaForm.vue';
  import resultForm from './resultForm.vue';
  import InputForm from './form.vue';
  import OAForm from './OAForm.vue';
  import { useGo } from '/@/hooks/web/usePage';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('process.job.pmConnectwaterjob');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    // value: router.currentRoute.value.meta.title || t('接水作业管理'),
    value: t('接水作业管理'),
  };

  const props = defineProps({
    projectno: { type: String, default: '' },
  });

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工程编码'),
        field: 'projectno',
        component: 'Input',
      },
      {
        label: t('工程名称'),
        field: 'projectname',
        component: 'Input',
      },
      // {
      //   label: t('创建时间'),
      //   field: 'createDate',
      //   component: 'Input',
      // },
      // {
      //   label: t('接水类别'),
      //   field: 'name',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'zf_conWater_type',
      //     allowClear: true,
      //   },
      // },

      // {
      //   label: t('oano'),
      //   field: 'oano',
      //   component: 'Input',
      // },
      // {
      //   label: t('oapic'),
      //   field: 'oapic',
      //   component: 'Input',
      // },
      // {
      //   label: t('发起人'),
      //   field: 'sponsor',
      //   component: 'Input',
      // },
      // {
      //   label: t('接水完成'),
      //   field: 'sponsordate',
      //   component: 'DatePicker',
      //   componentProps: {
      //     format: 'YYYY-MM-DD HH:mm',
      //     showTime: { format: 'HH:mm' },
      //   },
      // },
      // {
      //   label: t('停水时间'),
      //   field: 'connectwatertime',
      //   component: 'DatePicker',
      //   componentProps: {
      //     format: 'YYYY-MM-DD HH:mm',
      //     showTime: { format: 'HH:mm' },
      //   },
      // },
      // {
      //   label: t('ffile'),
      //   field: 'ffile',
      //   component: 'Input',
      // },
      // {
      //   label: t('接水结果'),
      //   field: 'connectwaterresult',
      //   component: 'Input',
      // },
      // {
      //   label: t('connectwaterpic'),
      //   field: 'connectwaterpic',
      //   component: 'Input',
      // },
      // {
      //   label: t('行政区域'),
      //   field: 'administrativearea',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'zf_Area',
      //     allowClear: true,
      //   },
      // },
      // {
      //   label: t('接水管道口径'),
      //   field: 'pipesize',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'zf_pipe_size',
      //     allowClear: true,
      //   },
      // },
      // {
      //   label: t('阀门位置'),
      //   field: 'valveposition',
      //   component: 'Input',
      // },
      // {
      //   label: t('停水工作位置'),
      //   field: 'cutoffarea',
      //   component: 'Input',
      // },
      // {
      //   label: t('影响户数'),
      //   field: 'population',
      //   component: 'Input',
      // },
      // {
      //   label: t('重点单位'),
      //   field: 'keyunit',
      //   component: 'Input',
      // },
      // {
      //   label: t('排水时长'),
      //   field: 'wdischargetime',
      //   component: 'Input',
      // },
      // {
      //   label: t('冲洗时长'),
      //   field: 'wsupplytime',
      //   component: 'Input',
      // },
      // {
      //   label: t('施工时间'),
      //   field: 'constructiontime',
      //   component: 'Input',
      // },
      // {
      //   label: t('OA表单ID'),
      //   field: 'wdischargeoano',
      //   component: 'Input',
      // },
      // {
      //   label: t('wdischargeoapic'),
      //   field: 'wdischargeoapic',
      //   component: 'Input',
      // },
      // {
      //   label: t('wastepipefile'),
      //   field: 'wastepipefile',
      //   component: 'Input',
      // },
      // {
      //   label: t('wastepipe'),
      //   field: 'wastepipe',
      //   component: 'Input',
      // },
      // {
      //   label: t('gwwastepipefile'),
      //   field: 'gwwastepipefile',
      //   component: 'Input',
      // },
      // {
      //   label: t('停水范围'),
      //   field: 'frontpressurearea',
      //   component: 'Input',
      // },
      // {
      //   label: t('startimg'),
      //   field: 'startimg',
      //   component: 'Input',
      // },
      // {
      //   label: t('worktime'),
      //   field: 'worktime',
      //   component: 'Input',
      // },
      // {
      //   label: t('pipeinvolved'),
      //   field: 'pipeinvolved',
      //   component: 'Input',
      // },
      // {
      //   label: t('接水现场图片'),
      //   field: 'scenepic',
      //   component: 'Input',
      // },
      // {
      //   label: t('duration'),
      //   field: 'duration',
      //   component: 'Input',
      // },
      // {
      //   label: t('管道直径'),
      //   field: 'diameter',
      //   component: 'Input',
      // },
      // {
      //   label: t('备注信息'),
      //   field: 'remarks',
      //   component: 'Input',
      // },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'bpm_biz_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'left',
    },
   
    // {
    //   title: t('接水类别'),
    //   dataIndex: 'name',
    //   key: 'a.name',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'zf_conWater_type',
    // },
    {
      title: t('工程编码'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'projectno',
    },
    {
      title: t('工程名称'),
      dataIndex: 'projectname',
      key: 'a.projectname',
      sorter: true,
      width: 200,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('申请时间'),
      dataIndex: 'createDate',
      key: 'a.createtime',
      sorter: true,
      width: 130,
      align: 'left',
      // fixed: 'left',
      slot: 'createDate',
    },

    {
      title: t('申请人'),
      dataIndex: 'sponsor',
      key: 'a.sponsor',
      sorter: true,
      width: 100,
      align: 'left',
    },

    {
      title: t('停水时间'),
      dataIndex: 'connectwatertime',
      key: 'a.connectwatertime',
      sorter: true,
      width: 150,
      align: 'center',
      slot: 'connectwatertime',
    },
    {
      title: t('接水完成时间'),
      dataIndex: 'sponsordate',
      key: 'a.sponsordate',
      sorter: true,
      width: 150,
      align: 'center',
      slot: 'sponsordate',
    },

    // {
    //   title: t('行政区域'),
    //   dataIndex: 'administrativearea',
    //   key: 'a.administrativearea',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'zf_Area',
    // },
    // {
    //   title: t('接水管道口径'),
    //   dataIndex: 'pipesize',
    //   key: 'a.pipesize',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'zf_pipe_size',
    // },
    // {
    //   title: t('阀门位置'),
    //   dataIndex: 'valveposition',
    //   key: 'a.valveposition',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('停水工作位置'),
    //   dataIndex: 'cutoffarea',
    //   key: 'a.cutoffarea',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('影响户数'),
    //   dataIndex: 'population',
    //   key: 'a.population',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('重点单位'),
    //   dataIndex: 'keyunit',
    //   key: 'a.keyunit',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('排水时长'),
    //   dataIndex: 'wdischargetime',
    //   key: 'a.wdischargetime',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('冲洗时长'),
    //   dataIndex: 'wsupplytime',
    //   key: 'a.wsupplytime',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('施工时间'),
    //   dataIndex: 'constructiontime',
    //   key: 'a.constructiontime',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('OA表单ID'),
    //   dataIndex: 'wdischargeoano',
    //   key: 'a.wdischargeoano',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('wdischargeoapic'),
    //   dataIndex: 'wdischargeoapic',
    //   key: 'a.wdischargeoapic',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('wastepipefile'),
    //   dataIndex: 'wastepipefile',
    //   key: 'a.wastepipefile',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('wastepipe'),
    //   dataIndex: 'wastepipe',
    //   key: 'a.wastepipe',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
    // {
    //   title: t('gwwastepipefile'),
    //   dataIndex: 'gwwastepipefile',
    //   key: 'a.gwwastepipefile',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('停水范围'),
    //   dataIndex: 'frontpressurearea',
    //   key: 'a.frontpressurearea',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('startimg'),
    //   dataIndex: 'startimg',
    //   key: 'a.startimg',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('worktime'),
    //   dataIndex: 'worktime',
    //   key: 'a.worktime',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('pipeinvolved'),
    //   dataIndex: 'pipeinvolved',
    //   key: 'a.pipeinvolved',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('接水现场图片'),
    //   dataIndex: 'scenepic',
    //   key: 'a.scenepic',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('duration'),
    //   dataIndex: 'duration',
    //   key: 'a.duration',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('管道直径'),
    //   dataIndex: 'diameter',
    //   key: 'a.diameter',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('备注信息'),
    //   dataIndex: 'remarks',
    //   key: 'a.remark',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
  ];

  const actionColumn: BasicColumn = {
    width: 140,
    align: 'left',
    actions: (record: Recordable) => [
      
      {
        icon: 'ant-design:eye-outlined',
        title: t('查看OA执行情况'),
        onClick: handleOpenOADrawer.bind(this, { projectno: record.projectno }),
        auth: 'process:job:pmConnectwaterjob:edit',
        //ifShow: () => record.status == '9',
      },
      {
        icon: 'fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
      
      {
        icon: 'mdi:checkbox-marked-outline',
        title: t('任务办理'),
        onClick: handleBpmMyTask.bind(this, { id: record.id }),
        ifShow: () => record.status == '4' || record.status == '5' || record.status == '6',
      },
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑接水作业'),
        onClick: handlecreateForm.bind(this, { id: record.id }),
        auth: 'process:job:pmConnectwaterjob:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除接水作业'),
        popConfirm: {
          title: t('是否确认删除接水作业'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'btn:delete:jieshuizuoye',
        ifShow: () => record.status == '9',
      },
      
      
    ],
  };

  const [registerDrawer, { openDrawer: openDrawer }] = useDrawer();
  const [registerOADrawer, { openDrawer: openOADrawer }] = useDrawer();
  const [registerresultDrawer, { openDrawer: openresultDrawer }] = useDrawer();
  const [registersendOaDrawer, { openDrawer: opensendOaDrawer }] = useDrawer();
  const [registermoreDrawer, { openDrawer: openmoreDrawer }] = useDrawer();
  const [registercreateDrawer, { openDrawer: opencreateDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: pmConnectwaterjobListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'radio',
    },
    immediate: false,
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }


  // 时间格式化日期，只保留年月日
  const formatDate = (date: string) => {
    if (!date) return '';
    const dateObj = new Date(date);
    const year = dateObj.getFullYear();
    const month = dateObj.getMonth() + 1;
    const day = dateObj.getDate();
    const h = dateObj.getHours();
    const m = dateObj.getMinutes();

    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day} ${h < 10 ? '0' + h : h}:${m < 10 ? '0' + m : m}`;
  };

  // OA审批
  function handleForm(record) {
    // // openresultDrawer(true, {})
    // if (getSelectRows().length) {
    //   // let selIds = ref('');
    //   // getSelectRows().forEach((item) => {
    //   //   selIds.value += item.id + ',';
    //   // });

    //   openDrawer(true, { id: getSelectRows()[0].id });
    //   // 接口
    // } else {
    //   showMessage('请先选择一条数据！');
    // }

    openDrawer(true, record);
  }

  // 上传接水结果
  function handleresultForm() {
    // openresultDrawer(true, {})
    if (getSelectRows().length) {
      // let selIds = ref('');
      // getSelectRows().forEach((item) => {
      //   selIds.value += item.id + ',';
      // });

      openresultDrawer(true, { id: getSelectRows()[0].id });
      // 接口
    } else {
      showMessage('请先选择一条数据！');
    }
  }

  // 推送OA审批
  function handlesendOaForm() {
    // opensendOaDrawer(true, {})
    if (getSelectRows().length) {
      // let selIds = ref('');
      // getSelectRows().forEach((item) => {
      //   selIds.value += item.id + ',';
      // });

      opensendOaDrawer(true, { id: getSelectRows()[0].id });
      // 接口
    } else {
      showMessage('请先选择一条数据！');
    }
  }

  // 完善信息
  function handlemoreForm() {
    // openmoreDrawer(true, {})

    if (getSelectRows().length) {
      // let selIds = ref('');
      // getSelectRows().forEach((item) => {
      //   selIds.value += item.id + ',';
      // });

      openmoreDrawer(true, { id: getSelectRows()[0].id });
      // 接口
    } else {
      showMessage('请先选择一条数据！');
    }
  }

  // 发起申请
  function handlecreateForm(record: Recordable) {
    opencreateDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmConnectwaterjobDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'Flow_ConnectWaterJob', bizKey: record.id });
  }

  const [registerDrawerBpmMyTask, { setDrawerData }] = useDrawer();
  const inputFormComponent = shallowRef<Nullable<any>>(null);
  const inputFormVisible = ref<Boolean>(false);

  async function handleBpmMyTask(record: Recordable) {
    const data = await bpmTaskDefForm({ formKey: 'Flow_ConnectWaterJob', bizKey: record.id });
    if (data.result == 'true') {
      const url = data.pcUrl;
      const idx = url.indexOf('?');
      // component
      let component: ReturnType<typeof defineComponent>;
      const compStr = idx == -1 ? url : url.substring(0, idx);
      if (compStr && compStr != '') {
        const imp = dynamicImport(compStr);
        if (imp) component = createAsyncComponent(imp);
      }
      // params
      let params = {};
      const paramStr = idx == -1 ? '' : url.substring(idx + 1);
      if (paramStr && paramStr != '') {
        params = qs.parse(paramStr);
      }
      // open
      if (component) {
        inputFormComponent.value = component;
        inputFormVisible.value = true;
        setDrawerData(params);
      }
    } else {
      showMessage(data.message);
    }
  }

  watch(
    () => props.projectno,
    () => {
      handleProjecte();
    },
  );

  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectno: props.projectno ? props.projectno : userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      projectno: props.projectno ? props.projectno : userStore.getProjecte.code,
    });
  }

  // 查看OA申请进度
  function handleOpenOADrawer(record: Recordable) {
    console.log(record);
    openOADrawer(true, record);
  }
</script>